﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using DataAccess;
using DAO;
namespace BUL
{
    public class PatientBUL
    {
        ConnectDatabase cd;
        public PatientBUL() {
            cd = new ConnectDatabase();
        }
        public DataTable loadTable() {
            return cd.ExcuteQuery("select idPatient as 'Mã BN',fullname as 'Họ tên',gender as 'Giới tính',age as 'Tuổi',phone as 'Điện thoại',address as 'Địa chỉ',marital as 'Hôn nhân' from patient");
        }
        public DataTable loadTableOn()
        {
            return cd.ExcuteQuery("select idPatient as 'Mã BN',fullname as 'Họ tên',gender as 'Giới tính',age as 'Tuổi',phone as 'Điện thoại',address as 'Địa chỉ',marital as 'Hôn nhân' from patient where idClinic!='0'");
        }
        public DataTable loadTableIn(String id)
        {
            return cd.ExcuteQuery("select idPatient as 'Mã BN',fullname as 'Họ tên',gender as 'Giới tính',age as 'Tuổi',phone as 'Điện thoại',address as 'Địa chỉ',marital as 'Hôn nhân' from patient where idWard!='0' and idWard='"+id+"'");
        }
        public DataTable loadTN(String id) {
            return cd.ExcuteQuery("select nextofkind.fullname,nextofkind.address,nextofkind.phone,nextofkind.relationship from patient,nextofkind where idPatient='" + id + "' and patient.idNextOfKind=nextofkind.idNextOfKind");
        }
        public Boolean InsertBN(PatientDAO p) {
            String sql = @"insert into patient (fullname, gender, age, phone, marital, address,idNextOfKind,idClinic) values(@name,@gender,@age,@phone,@marital,@address,@id,@idClinic)";
            int count = 8;
            String[] name = new String[count];
            object[] value = new object[count];
            name[0] = "@name";
            name[1] = "@gender";
            name[2] = "@age";
            name[3] = "@phone";
            name[4] = "@marital";
            name[5] = "@address";
            name[6] = "@id";
            name[7] = "@idClinic";
            value[0] = p.Fullname;
            value[1] = p.Gender;
            value[2] = p.Age;
            value[3] = p.Phone;
            value[4] = p.Marital;
            value[5] = p.Address;
            value[6] = p.IdNextOfKind;
            value[7] = p.IdClinic;
            return cd.ExcuteNonQuery(sql, name, value, count);
        }
        public Boolean updateBN(PatientDAO p)
        {
            String sql = @"update  patient set fullname=@name, gender=@gender, age=@age, phone=@phone, marital=@marital, address=@address,idClinic = @idClinic where idPatient=@id";
            int count = 8;
            String[] name = new String[count];
            object[] value = new object[count];
            name[0] = "@name";
            name[1] = "@gender";
            name[2] = "@age";
            name[3] = "@phone";
            name[4] = "@marital";
            name[5] = "@address";
            name[6] = "@id";
            name[7] = "@idClinic";
            value[0] = p.Fullname;
            value[1] = p.Gender;
            value[2] = p.Age;
            value[3] = p.Phone;
            value[4] = p.Marital;
            value[5] = p.Address;
            value[6] = p.IdPatient;
            value[7] = p.IdClinic;
            return cd.ExcuteNonQuery(sql, name, value, count);
        }
        public Boolean deleteBN(String id)
        {
            String sql = @"delete from patient where idPatient = '" + id + "'";
            return cd.ExcuteNonQuery(sql);
        }
        
    }
}
